home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / communic / pcmail / main / deskutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  7.3 KB  |  279 lines

  1. /*++
  2. /* NAME
  3. /*    deskutil 3
  4. /* SUMMARY
  5. /*    utility functions
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    mail
  10. /* SYNOPSIS
  11. /*    #include "mail.h"
  12. /*
  13. /*    void patience()
  14. /*
  15. /*    int when()
  16. /*
  17. /*    int delete()
  18. /*
  19. /*    int unspool()
  20. /*
  21. /*    int print()
  22. /*
  23. /*    int save()
  24. /*
  25. /*    int filter()
  26. /*
  27. /*    char *tstamp(ltime)
  28. /*    long *ltime()
  29. /* DESCRIPTION
  30. /*    tstamp() converts absolute time to a string. If called with a
  31. /*    recent time argument (less than 100 days ago) the string will
  32. /*    be of the form "Sun Apr 17 12:50", otherwise "Sun Apr 17  1988".
  33. /*
  34. /*      delete() gives the user another chance before a message is deleted.
  35. /*
  36. /*      unspool() actually deletes a message. As a side effect it destroys
  37. /*      the current mail box display so that the next display will
  38. /*    reflect the actual status of the spool directory.
  39. /*    The affected message and meta file names are taken from the
  40. /*    global "message" and "commant" string variables.
  41. /*
  42. /*    print() copies a pager file to the printer.
  43. /*
  44. /*    save() asks where the pager file should be saved.
  45. /*
  46. /*    filter() asks the command through which the file being shown should be
  47. /*    piped. The original file is used.
  48. /*
  49. /*    when() should be called after the user has entered a mail destination
  50. /*    address. It informs the user that messages are not sent right away, 
  51. /*    but after selection of the Network option in the main menu. This
  52. /*    function is a no-op on systems that use daemons for message transport.
  53. /*
  54. /*    patience() prints a 'one moment please' message in the middle
  55. /*    screen window. As a side effect, the current pager file is set
  56. /*    to none.
  57. /* FILES
  58. /*      mail header files in the spool directory
  59. /* SEE ALSO
  60. /*      pager(3), pager(5), kbdinp(3)
  61. /* AUTHOR(S)
  62. /*      W.Z. Venema
  63. /*      Eindhoven University of Technology
  64. /*      Department of Mathematics and Computer Science
  65. /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  66. /* CREATION DATE
  67. /*    Tue May 12 15:35:20 GMT+1:00 1987
  68. /* LAST MODIFICATION
  69. /*    90/01/22 13:01:32
  70. /* VERSION/RELEASE
  71. /*    2.1
  72. /*--*/
  73.  
  74. #include <stdio.h>
  75. #include <errno.h>
  76. #include <time.h>
  77.  
  78. #include "defs.h"
  79. #include "pager.h"
  80. #include "mail.h"
  81. #include "screen.h"
  82. #include "status.h"
  83.  
  84. extern struct tm *localtime();        /* std C library */
  85.  
  86. hidden int save_util();
  87. hidden int pipe_util();
  88.  
  89. /* patience - say this will take some time */
  90.  
  91. public void patience()
  92. {
  93.     static char *m_wait[] = {
  94.     "",
  95.     "One moment please...",
  96.     0,
  97.     };
  98.  
  99.     register File *pp = open_pager();        /* create pager file */
  100.  
  101.     mesg_pager(pp,m_wait);            /* write pager file */
  102.     ds_pager();                    /* show on middle window */
  103.     close_pager(pp);                /* forget pager file */
  104. }
  105.  
  106. /* delete - user wants to delete a message; ask for confirmation */
  107.  
  108. public int delete()
  109. {
  110.     static Screen screen[] = {
  111.     ESCCR,    "Enter",    unspool,int_error,
  112.     0,    0,        0,
  113.     "Press ESC to cancel. Confirm with ENTER",
  114.     };
  115.  
  116.     return(kbdinp(screen)|S_REDRAW);
  117. }
  118.  
  119. /* unspool - actually delete a message; force mail box display rebuild */
  120.  
  121. public int unspool()
  122. {
  123.     if (((chmod(message,0666) || unlink(message)) && errno != ENOENT)
  124.     || ((chmod(comment,0666) || unlink(comment)) && errno != ENOENT)) {
  125.     errdisp(E_UNLINK);            /* notify user of problem */
  126.     return(S_REDRAW);            /* say screen has changed */
  127.     } else {
  128.     junk_desk();                /* say mail box has changed */
  129.     return(S_BREAK);            /* no more work to do */
  130.     }
  131. }
  132.  
  133. /* print - print pager display on default printer */
  134.  
  135. public int print()
  136. {
  137.     return(pr_pager() ? (errdisp(E_PRINTERR),S_REDRAW) : 0);
  138. }
  139.  
  140. /* save - ask where pager display should be copied to */
  141.  
  142. public int save()
  143. {
  144.     static Screen screen[] = {
  145.     STRING,    0,              save_util,int_error,
  146.     0,    0,              0,
  147.     "Press ESC to cancel. Save to file:",
  148.     };
  149.  
  150.     kbdinp(screen);            /* prompt for file name, then copy */
  151.     return(S_REDRAW);            /* force screen repaint */
  152. }
  153.  
  154. /* save_util - copy pager file to ordinary file */
  155.  
  156. hidden int save_util(to)
  157. char *to;
  158. {
  159.     if (cp_pager(to)) {            /* if file copy failed */
  160.     unlink(to);            /* remove that file */
  161.     errdisp(E_WRITERR);        /* notify the user */
  162.     return(S_BREAK|S_REDRAW);    /* redisplay, terminate caller */
  163.     } else {
  164.     junk_file();            /* say file display maybe outdated */
  165.     return(S_BREAK);        /* terminate caller */
  166.     }
  167. }
  168.  
  169. /* filter - ask command through which the pager display should be piped */
  170.  
  171. public int filter()
  172. {
  173.     static Screen screen[] = {
  174.     STRING,    0,              pipe_util,int_error,
  175.     0,    0,              0,
  176.     "Press ESC to cancel. Filter through command:",
  177.     };
  178.  
  179.     kbdinp(screen);            /* prompt for file name, then copy */
  180.     return(S_REDRAW);            /* force screen repaint */
  181. }
  182.  
  183. /* redirect - make old refer to new, close redundant file descriptors */
  184.  
  185. hidden int redirect(old, new)
  186. int     old;
  187. int     new;
  188. {
  189.     register int ret;
  190.  
  191.     (void) close(old);
  192.     ret = dup(new);
  193.     (void) close(new);
  194.     return (ret != old);
  195. }
  196.  
  197. /* pipe_util - pipe external file through command */
  198.  
  199. hidden int pipe_util(cmd)
  200. char   *cmd;
  201. {
  202.     register int fd_stdin;
  203.     register int fd_msg;
  204.  
  205.     /*
  206.      * Run command with standard input redirected to the file displayed, and
  207.      * clean up. Note that the original file is used, not the contents of the
  208.      * pager. We use system(3), so that the command can be a pipeline. This
  209.      * code is a bit messy, since it has to run both with UNIX and MS-DOS.
  210.      */
  211.  
  212.     if ((fd_msg = open(message, 0)) < 0) {
  213.     errdisp(E_READERR);
  214.     } else {
  215.     kbdrest();                /* cooked-mode input */
  216.     if ((fd_stdin = dup(0)) < 0) {        /* save standard input */
  217.         kbdinit();                /* raw-mode input */
  218.         errdisp(E_SYSFAIL);
  219.     } else {
  220.         (void) putchar('\n');
  221.         if (redirect(0, fd_msg))        /* re-direct standard input */
  222.         fatal("cannot re-direct standard input");
  223.         (void) system(cmd);            /* try to execute command */
  224.         if (redirect(0, fd_stdin))        /* restore standard input */
  225.         fatal("cannot restore standard input");
  226.         kbdinit();                /* raw-mode input */
  227.         (void) printf(anykey);
  228.         (void) getkey();
  229.         junk_file();            /* file display outdated */
  230.     }
  231.     }
  232.     return (S_BREAK | S_REDRAW);        /* terminate caller */
  233. }
  234.  
  235. /* when - say when mail will actually be sent */
  236.  
  237. public int when()
  238. {
  239. #ifndef    DAEMON
  240.     static char *msg[] = {
  241.     "",
  242.     "To send messages through the network, use the Network",
  243.     "option in the main menu.",
  244.     0,
  245.     };
  246.     File *pp = open_pager();            /* open new pager file */
  247.  
  248.     mesg_pager(pp,msg);                /* fill pager file */
  249.     ds_pager();                    /* display the file */
  250.     close_pager(pp);                /* forget pager file */
  251. #endif
  252.     return(0);                    /* don't care value */
  253. }
  254.  
  255. /* tstamp - time format as produced by the ls(1) command */
  256.  
  257. public char *tstamp(ltime)
  258. long   *ltime;
  259. {
  260.     static char buf[25];
  261.  
  262.     /*
  263.      * Output from asctime() is of the form
  264.      * 
  265.      * "Sun Apr 17 13:34:35 1988\n"
  266.      * 
  267.      * Depending on how recent the time in question is, we supress the time or
  268.      * year field. The weekday field is always suppressed.
  269.      */
  270.  
  271.     (void) strcpy(buf, asctime(localtime(ltime)));
  272.     if (time((long *) 0) - *ltime > 60L * 60L * 24L * 100L) {
  273.     buf[24] = '\0';                /* remove the \n */
  274.     (void) strcpy(buf + 11, buf + 19);    /* old, show year */
  275.     } else
  276.     buf[16] = '\0';                /* recent, show time */
  277.     return (buf + 4);
  278. }
  279.